package cn.tedu.blog.server.service.impl;



import cn.tedu.blog.server.exception.ServiceException;
import cn.tedu.blog.server.mapper.LinkMapper;
import cn.tedu.blog.server.pojo.dto.LinkAddNewDTO;
import cn.tedu.blog.server.pojo.dto.LinkEditDTO;
import cn.tedu.blog.server.pojo.entity.Link;
import cn.tedu.blog.server.pojo.vo.CategoryListVO;
import cn.tedu.blog.server.pojo.vo.LinkDetailVO;
import cn.tedu.blog.server.pojo.vo.LinkListItemVO;
import cn.tedu.blog.server.repo.impl.LinkRepository;
import cn.tedu.blog.server.service.ILinkService;
import cn.tedu.blog.server.utils.OssUtils;
import cn.tedu.blog.server.utils.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static cn.tedu.blog.server.repo.ICategoryRepository.KEY_CATEGORY_LIST;
import static cn.tedu.blog.server.repo.ILinkRepository.KEY_Link_LIST;

@Service
@Slf4j
public class LinkServiceImpl implements ILinkService {
    @Autowired
    private LinkMapper linkMapper;

    @Autowired
    private LinkRepository linkRepository;

    @Override
    public void addNew(LinkAddNewDTO linkAddNewDTO) {
        log.debug("开始处理添加友链的业务, 参数:{}", linkAddNewDTO);
        // 先查询有无名称相同/路径相同的友链, 如果有则抛出异常
        String name = linkAddNewDTO.getName();
        LinkDetailVO linkDetailVO = linkMapper.getByName(name);
        if (linkDetailVO != null) {
            String message = "添加友链失败, 友链名称[" + name + "]已存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        String url = linkAddNewDTO.getUrl();
        linkDetailVO = linkMapper.getByUrl(url);
        if (linkDetailVO != null) {
            String message = "添加友链失败, 友链路径[" + url + "]已存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        //
        Link link = new Link();
        BeanUtils.copyProperties(linkAddNewDTO, link);
        // 补全属性

        LocalDateTime create = LocalDateTime.now();
        link.setGmtCreate(create);
        int rows = linkMapper.insert(link);

        if (rows != 1) {
            String message = "添加友链失败, 服务器忙, 请稍后再试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }

    @Override
    public void deleteById(long id) {
        log.debug("开始处理根据id删除友链的业务, id={}", id);
        // 先查询将要删除的友链是否存在
        LinkDetailVO linkDetailVO = linkMapper.getById(id);
        if (linkDetailVO == null) {
            String message = "删除失败, 尝试删除的友链(id=" + id + ")不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        int rows = linkMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除品牌失败,服务器忙,请稍后再试!";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_UNKNOWN, message);
        }
    }

    @Override
    public int deleteByIds(Long[] ids) {
        List<LinkDetailVO> list=linkMapper.selectByIds(ids);
        for (LinkDetailVO detailVO : list) {
            String name = detailVO.getName();
            OssUtils.delete(name);
        }
        log.debug("开始处理批量删除友链业务, ids={}", ids);
        int rows = linkMapper.deleteByIds(ids);
        return rows;
    }

    @Override
    public void updateById(long id, LinkEditDTO linkEditDTO) {
        log.debug("开始处理根据id, 修改文章的业务, id={}, linkEditDTO={}", id, linkEditDTO);
        // 先查询此文章是否存在, 不存在则抛异常
        LinkDetailVO linkDetailVO = linkMapper.getById(id);
        if (linkDetailVO == null) {
            String message = "修改友链失败, 友链修改的友链id=(" + id +")不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

        Link link = new Link();
        BeanUtils.copyProperties(linkEditDTO, link);
        link.setId(id);
        LocalDateTime modified = LocalDateTime.now();
        link.setGmtModified(modified);
        int rows = linkMapper.updateById(link);
        if (rows != 1) {
            String message = "修改友链数据失败, 服务器忙, 请稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }


    @Override
    public LinkDetailVO getByName(String name) {
        log.debug("开始处理根据名称查询友链的业务, name={}", name);
        return linkMapper.getByName(name);
    }

    @Override
    public List<LinkListItemVO> list() {
        log.debug("开始处理查询友链列表的业务");
        return linkMapper.list();
    }

    @Override
    public List<LinkListItemVO> listByLinks(LinkListItemVO linkListItemVO) {
        log.debug("准备开始处理条件分页查询类别的业务，参数：{}", linkListItemVO);
        return linkMapper.listByLink(linkListItemVO);
    }

    @Override
    public int getCount(LinkListItemVO linkListItemVO) {
        log.debug("准备开始处理条件查询结果集数量的业务，参数：{}", linkListItemVO);
        return linkMapper.getCount(linkListItemVO);
    }



    /*前台新加友链展示*/
    @Override
    public List<LinkListItemVO> newList() {
        boolean isExpired= linkRepository.isExpired(KEY_Link_LIST);
        if(isExpired){
            log.debug("====执行数据库查询存入redis====");
            List<LinkListItemVO> list =linkMapper.listAll();
            if(list==null){
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"数据库中没有分类");
            }
            linkRepository.putList(list,KEY_Link_LIST);
            return list;
        }else {
            return linkRepository.getList(KEY_Link_LIST);
        }
    }

}
